package cn.yanghan.controller;

import cn.yanghan.doc.CourseDoc;
import cn.yanghan.dto.CourseSearchParamDto;
import cn.yanghan.repository.CourseElasticsearchRepository;
import cn.yanghan.service.ICourseElasticsearchService;
import cn.yanghan.utils.util.AjaxResult;
import cn.yanghan.utils.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//针对于课程的ES API
@RestController
@RequestMapping("/course")
public class CourseEsController {

    @Autowired
    private ICourseElasticsearchService courseElasticsearchService;

    //添加课程到ES
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('es:saveCourse')")
    public AjaxResult add(@RequestBody CourseDoc courseDoc){
        //添加课程文档
        courseElasticsearchService.add(courseDoc);
        return AjaxResult.me();
    }

    //搜索课程
    @PostMapping("/searchCourse")
    public AjaxResult searchCourse(@RequestBody CourseSearchParamDto dto){
        //添加课程文档
        PageList<CourseDoc> pageList = courseElasticsearchService.searchCourse(dto);
        return AjaxResult.me().setResultObj(pageList);
    }

}
